// pages/页面1.js
const innerAudioContext = wx.createInnerAudioContext()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    search:"",
    songs:[],
    music:'',
    _show:-1,
    pause:true,
    song_id:-1,
    show_bottom:"none",
    show_menu_play:"none",
    now_song_index:0,
    more_view:true,
    //初始详细页面的图片地址
    music_photo: 'https://s1.ax1x.com/2020/03/23/8HkVJS.png',
    //音乐现在的百分比进度
    music_long:0,
    music_all_long:0

    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    var that=this
    innerAudioContext.onEnded(function () {
      that.next_song()
      console.log("播放下一首")
      that.setData({
        music_long:0
      })
    })
    innerAudioContext.onPlay(function(){
      that.change_music_image()
      setTimeout(() => {
        innerAudioContext.currentTime
        innerAudioContext.onTimeUpdate(() => {
          // console.log(parseInt(innerAudioContext.duration))   //总时长
          // console.log(parseInt(innerAudioContext.currentTime))   //当前播放进度
          
          var now=parseInt( parseInt(innerAudioContext.currentTime) / parseInt(innerAudioContext.duration)*100)
          // 所占的百分比
          if(that.data.music_long!=now){
            that.setData({
              music_long:now
            })
          }
          if(that.data.music_all_long!=parseInt(innerAudioContext.duration)){
            that.setData({
              music_all_long:parseInt(innerAudioContext.duration)
            })
          }
        })
      }, 500)  
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  
  search_song:function(){
    innerAudioContext.stop()
    
    var that = this//不要漏了这句，很重要
    that.setData({
      songs: [],
      music: '',
      _show: -1,
      pause: true,
      song_id: -1,
      show_bottom: "none",
      show_menu_play: "none",
      now_song_index: 0,
      more_view: true,

    })
    wx.request({
      url: 'https://autumnfish.cn/search?keywords='+this.data.search,
      headers: {
        'Content-Type': 'application/json'
      },
      success(res) {
        that.setData({
          songs: res.data.result.songs
        })
        setTimeout(function() {
          console.log(that.data.songs)
          that.setData({
            show_bottom:"flex"
          })
          }
        ,1000)   
      }
    })
  },
  play:function(e){
    var that = this
    this.data.now_song_index = e.target.dataset.index  
    
    //切换图标样式
    
    if (that.data._show == e.target.dataset.index){
      that.setData({
        _show:9999,
        
      })
    }else{
      that.setData({
        _show:e.target.dataset.index
      })
      console.log(that.data._show)
    }
    //如果正在播放的id和点击的是统一首歌,就进行暂停操作
    if(that.data.song_id==e.target.dataset.id){
      //判断是否处于暂停状态
      if(that.data.pause){
        console.log("继续播放")
        that.setData({
          pause:!that.data.pause
        })
        innerAudioContext.play()
      }
      else{
        console.log("暂停播放")
        that.setData({
          pause: !that.data.pause
        })
        innerAudioContext.pause()
      }
      
    }
    //切换歌曲
    else{
      console.log("切换歌曲")
      // 获取歌曲并播放
      wx.request({
        url: 'https://autumnfish.cn/song/url?id=' + e.target.dataset.id,
        headers: {
          'Content-Type': 'application/json'
        },
        success(res) {
          let url = res.data.data[0].url
          innerAudioContext.autoplay = true
          innerAudioContext.src = url
          innerAudioContext.play()
        }
      })
      that.setData({
        song_id:e.target.dataset.id,
        pause:false
      })
    }
    that.setData({
      show_menu_play: "display"
    })  
    
    

  },
  change_search:function(value){
    console.log(value.detail.value)
    this.setData({
      search: value.detail.value
    })
  },
  //点击菜单的播放暂停
  menu_play:function(){
    if (this.data.pause) {
      console.log("继续播放")
      this.setData({
        pause: !this.data.pause,
        _show:this.data.now_song_index
      })
      innerAudioContext.play()
    }
    else {
      console.log("暂停播放")
      this.setData({
        pause: !this.data.pause,
        _show:9999
      })
      innerAudioContext.pause()
    }
  },
  //点击下一首
  next_song:function(){
    var now_index=0
    var that = this
    this.data.songs.forEach((item,index)=>{
      if (that.data.song_id==item.id){
        now_index=index
        
        return
      }
    })
    //判断是否是第一首或者最后一首
    if(now_index==that.data.songs.length-1){
      console.log('这是最后一首')
      return
    }
    //取出下一首的id
    var next_song_id=this.data.songs[now_index+1].id
    //如果是第二个参数是+,就是播放下一首,-就是播放上一首
    this.play_music(next_song_id,"+")

  },
  //切换上一首下一首的方法
  play_music(id,decide){
    var that=this
    that.setData({
      show_menu_play:"block"
    })
    wx.request({
      url: 'https://autumnfish.cn/song/url?id=' +id,
      headers: {
        'Content-Type': 'application/json'
      },
      success(res) {
        let url = res.data.data[0].url
        innerAudioContext.autoplay = true
        innerAudioContext.src = url
        innerAudioContext.play()
      }
    })
    if(decide=="+"){
      that.setData({
        _show: that.data.now_song_index + 1,
        song_id: id,
        pause: false,
        now_song_index: that.data.now_song_index + 1
      })
    }else{
      that.setData({
        _show: that.data.now_song_index - 1,
        song_id: id,
        pause: false,
        now_song_index: that.data.now_song_index - 1
      })
    }
    
  },
  //点击上一首
  before_song: function () {
    var now_index
    var that = this
    this.data.songs.forEach((item, index) => {
      if (that.data.song_id == item.id) {
        now_index = index

        return
      }
    })
    //判断是否是第一首或者最后一首
    if (now_index == 0) {
      console.log('这是第一首')
      return
    }
    //取出下一首的id
    var next_song_id = this.data.songs[now_index - 1].id
    //如果是第二个参数是+,就是播放下一首,-就是播放上一首
    this.play_music(next_song_id, "-")

  },
  // change_view:function(){
  //   let song = this.data.songs[this.data.now_song_index]
  //   let data=JSON.stringify(this.data)
  //   wx.navigateTo({
  //     url: `/pages/about/about?pause=${this.data.pause}&song_name=${song.name}&song_id=${song.id}`
  //   })
  // }
  //切换详情页
  goto_more_view:function(e){
    var that = this
    if(that.data.pause){
      this.play(e)
    }
    
    
    
    that.setData({
      more_view:false
    })
    console.log(that.data.music_photo)
  },
  //切换列表页
  back:function(){
    this.setData({
      more_view:true
    })

  },
  //切换音乐图片
  change_music_image:function(){
    var that=this
    wx.request({
      url: 'https://autumnfish.cn/song/detail?ids=' + that.data.song_id,
      headers: {
        'Content-Type': 'application/json'
      },
      success(res) {
        console.log(res)
        that.setData({
          music_photo: res.data.songs["0"].al.picUrl
        })
      }
    })
  },
  //监听拖动条的值
  listenerSlider:function(e) {
    //获取滑动后的值
    console.log("滑动条的值"+e.detail.value);
    this.setData({
      music_long:e.detail.value
    })
    var jump=Math.round(e.detail.value/100*that.data.music_all_long) 
    innerAudioContext.seek(jump)

   },
  
})